const app = getApp();
import { zg99Request } from "/utils/requests";
Page({
    data: {
        showContact: true,
        suspendShow: true,
        showIcon: true,
        bgcolor: '',
        bookDetails_box: !1,
        isSydw: false,
        myPhone: "",
        showIcon: !0,
        yl: 1,
        ziliaobox: 0,
        isSydw: false,
        login: true,
        chaxunbox: 0,
        gwlb: null,
        isShowAuth2: !1,
        wangke: false,
        notice: false,
        inputPhone: false,
        choiceGG: false,
        notices: false,
    },
    async onLoad(options){
        this.setData(await app.methods.myPhone()); // 获取本地存储手机号
        if(!this.data.myPhone) {
            await app.methods.getPhoneNumber()
            this.setData({
              isShowAuth: !0
            })
        }
        // this.setData({
        //     myPhone: null
        // })
        xhs.showLoading({
            title: '数据加载中....',
            mask: true
        })
        xhs.showShareMenu({
          shareTypes: 'normal',
        })
        let links, jxOptions;
        if (options.scene) {
            options.scene = options.scene.replace(/%253D/g, ".");
            options.scene = options.scene.replace(/%2526/g, "-");
            jxOptions = await app.methods.getOPtions(options);
        }
        let id = options.scene ? jxOptions.id : options.id;
        options.id = id;
        links = await app.methods.getLink(id);
        options.xm = links.xm ? links.xm : '';
        options.yl = links.yl ? links.yl : '';
        if(links.actid) {
            options.actid = links.actid
        }
        options.fx = options.scene ? jxOptions.fx : options.fx;
        options.gh = options.scene ? jxOptions.gh : options.gh;
        this.setData({
            xm: options.xm
        })
        let xiangmu = options.xm;
        if (xiangmu.indexOf('sydw') >= 0) {
            this.setData({
                isSydw: true
            })
        }
        if (options.yl && options.yl.length > 0) {
            this.setData({
                yl: Number(options.yl)
            })
        }
        this.setData({
            options: options,
        })
        await this.getHdInfo(xiangmu);
        this.setData(await app.methods.getIdentity(options)); // 获取个人身份信息
        if(this.data.getIdentity.crm) {
            options.wxh = this.data.getIdentity.crm;
        } else {
            options.wxh = options.gh;
        }
        this.setData(await app.methods.getWechat(options)); // 获取个人身份信息
        this.setData(await app.methods.guishudi(options)); // 获取归属地
        let fenbu = this.data.fenbu;
        let fenxiao = this.data.fenxiao;
        let getIdentity = this.data.getIdentity;
        getIdentity.fenbu = fenbu;
        getIdentity.fenxiao = fenxiao;
        getIdentity.zzname = this.data.zzname;
        getIdentity.zzid = this.data.zzid;
        getIdentity.erpdqm = this.data.erpdqm;
        this.setData({
            options: options,
            navTop: app.globalData.navTop,
            navHeight: app.globalData.navHeight - app.globalData.navTop,
            getIdentity: getIdentity
        })
        if(this.data.myPhone) {
            this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, 'chaxun'));
            this.thisMethod();
        }
        xhs.showLoading({
            title: '数据加载完毕！',
        })
        setTimeout(function () {
            xhs.hideLoading()
        }, 1000)
        console.log(this.data)
    },
    // 加载内容
    async getHdInfo(xiangmu) {
        return new Promise((resolve) => {
            zg99Request({
                url: '/index/chaxun/getfzinfo/?actid=45681&callback=&',
                method: 'GET',
                dataType: 'jsonp',
                data: {
                    xiangmu,
                    time: Date.parse(new Date()) / 1000,
                },
            }).then(res => {
                let newStr = app.methods.getAPIReturn(res);
                if (newStr.status == 1) {
                    this.setData({
                        item: newStr.lists[0],
                        laiyuan: newStr.lists[0].laiyuan,
                        title: newStr.lists[0].title,
                        form: this.data.options.actid ? Number(this.data.options.actid) : Number(newStr.lists[0].form),
                        imgUrl: !newStr.lists[0].imgUrl ? this.data.imgUrl : newStr.lists[0].imgUrl,
                        notices: newStr.lists[0].notice,
                    })
                    app.globalData.laiyuan = `${newStr.lists[0].title}${newStr.lists[0].laiyuan}`
                }
                resolve()
            }).catch(err => {
                console.log(err)
                resolve()
            })
        })
    },
    // 该怎么就怎么
    async thisMethod() {
        if (!this.data.user) {
            if ((this.data.isSydw || this.data.isYiShi) && this.data.myPhone != '') {
                this.setData({
                    bookDetails_box: 1
                })
            } else if ((!this.data.isSydw || !this.data.isYiShi) && this.data.myPhone) {
                await app.methods.registerCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, '', '', this.data.book, 'chaxun')
                this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, 'chaxun'))
            }
        } else {
            if ((!this.data.user.user.gwlb || this.data.user.user.gwlb == 'null' || this.data.user.user.gwlb == 'null ' || this.data.user.user.gwlb == '') && (this.data.isSydw || this.data.isYiShi)) {
                this.setData({
                    bookDetails_box: 1
                })
            } else {
                await app.methods.updateuserCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, this.data.user.user.gwlb, '', 'chaxun', this.data.book, this.data.user.user)
                this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, 'chaxun'))
            }
        }
    },
    // 点击按钮
    async look(e){
        // console.log(e)
        if (e.currentTarget.dataset.method == 'ziliao') {
            await this.ziliao(e)
        } else {
            await this.chaxun(e)
        }
        // const { method, id, zltype, zllink, zlappid, ggtype, gglink, ggappid } = e.currentTarget.dataset;
        // if(!this.data.myPhone) {
        //     this.setData({
        //         isShowAuth: !0,
        //     });
        //     return false;
        // }
        // let type = method == 'ziliao' ? zltype : ggtype;
        // let link = method == 'ziliao' ? zllink : gglink;
        // let appid = method == 'ziliao' ? zlappid : ggappid;
        // await this.thisMethod();
        // if (link.indexOf(',') >= 0) {
        //     let ggLists = this.processString(link)
        //     this.setData({
        //         ggLists,
        //         choiceGG: true
        //     })
        //     return false;
        // } else if(type == 'pdf' && /files\.offcn\.com/.test(link)) {
        //     xhs.navigateTo({
        //         url: `/packageA/download/download?name=附件&url=${link}&type=file`
        //     })
        // } else if(type == 'pdf' && !/files\.offcn\.com/.test(link)) {
        //     xhs.navigateTo({
        //         url: '/pages/zixun/zixun?src=' + link + '&type=pdf',
        //     })
        // } else if (type == 'gonggao' && link != '' && /offcn\.com/.test(link)) {
        //     xhs.navigateTo({
        //         url: `/packageB/gg/gg?link=${link}`,
        //     })
        // } else if (type == 'html' && link != '') {
        //     xhs.navigateTo({
        //         url: '/pages/zixun/zixun?src=' + link,
        //     })
        // } else if (type == 'copy' && link != '') {
        //     this.setData({
        //         chaxunbox: 1,
        //         nowID: id
        //     })
        // } else if (type == 'xcx' && link != '') {
        //     xhs.navigateTo({
        //         url: `${link}&gh=${this.data.options.gh}&fx=${this.data.options.fx}&wxh=${this.data.options.wxh}`
        //     })
        // } else if (type == '19xcx' && link != '') {
        //     if (appid == 'wxca86930ec3e80717') {
        //         let { owner, orgn, erp } = this.data.getIdentity
        //         erp = erp ? erp : 12;
        //         orgn = orgn ? orgn : 13;
        //         owner = owner ? owner : 20588;
        //         if (link.indexOf('&') >= 0) {
        //             link = link.substring(0, link.indexOf('&'))
        //         }
        //         link = `${link}&owner=${owner}&orgn=${orgn}&erp=${erp}`
        //     }
        //     xhs.navigateToMiniProgram({
        //         appId: appid,
        //         path: link,
        //         envVersion: 'release'
        //     })
        // }
    },
    // 资料
    async ziliao(e) {
        let { zltype, zllink, id, zlappid } = e.currentTarget.dataset;
        let item = {
            type: zltype,
            link: zllink,
            id: id,
            appid: zlappid,
        }
        if (this.data.myPhone) {
            if (this.data.user != undefined) {
                await app.methods.updateuserCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, this.data.gwlb, '', 'chaxun', '', this.data.user.user)
                this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, 'chaxun'))
            } else {
                await app.methods.registerCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, this.data.gwlb, '', this.data.book, 'chaxun')
                this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, 'chaxun'))
            }
            this.typeAfter(item)
        } else {
            // if (zltype == 'pdf' || zltype == 'html' || zltype == 'copy') {
            //     this.setData({
            //         notice: true,
            //     })
            // } else {
            //     this.typeAfter(item)
            // }
            this.setData({
                isShowAuth: true,
            })
        }
    },
    // 资料
    async chaxun(e) {
        let { ggtype, gglink, id, ggappid } = e.currentTarget.dataset;
        // console.log(ggtype, gglink, id, ggappid)
        let item = {
            type: ggtype,
            link: gglink,
            id: id,
            appid: ggappid,
        }
        if (this.data.myPhone) {
            if (this.data.user != undefined) {
                await app.methods.updateuserCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, this.data.gwlb, '', 'chaxun', '', this.data.user.user)
                this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, 'chaxun'))
            } else {
                await app.methods.registerCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, this.data.gwlb, '', this.data.book, 'chaxun')
                this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, 'chaxun'))
            }
            this.typeAfter(item)
        } else {
            // if (ggtype == 'pdf' || ggtype == 'html' || ggtype == 'copy') {
            //     this.setData({
            //         notice: true,
            //     })
            // } else {
            //     this.typeAfter(item)
            // }
            this.setData({
                isShowAuth: true,
            })
        }
    },
    // 右侧按钮根据链接类型做出相应判断
    typeAfter(item) {
        let {
            type,
            link,
            id,
            appid
        } = item
        if (type === 'pdf' && /files\.offcn\.com/.test(link)) {
            if (link.indexOf(',') >= 0) {
                let ggLists = this.processString(link)
                this.setData({
                    ggLists,
                    choiceGG: true
                })
            } else {
                xhs.navigateTo({
                    url: `/packageA/download/download?name=附件&url=${link}&type=file`
                })
            }
        } else if (type === 'pdf' && !/files\.offcn\.com/.test(link)) {
            xhs.navigateTo({
                url: '/pages/zixun/zixun?src=' + link + '&type=pdf',
            })
        } else if (type == 'gonggao' && link != '' && /offcn\.com/.test(link)) {
            if (link.indexOf(',') >= 0) {
                let ggLists = this.processString(link)
                this.setData({
                    ggLists,
                    choiceGG: true
                })
            } else {
                xhs.navigateTo({
                    url: `/packageB/gg/gg?link=${link}`,
                })
            }
        } else if (type == 'html' && link != '') {
            xhs.navigateTo({
                url: '/pages/zixun/zixun?src=' + link,
            })
        } else if (type == 'copy' && link != '') {
            this.setData({
                chaxunbox: 1,
                nowID: id
            })
        } else if (type == 'xcx' && link != '') {
          if (link.indexOf(',') >= 0) {
            let ggLists = this.processString(link)
            this.setData({
                ggLists,
                choiceGG: true
            })
          } else {
            xhs.navigateTo({
                url: link + '&gh=' + this.data.options.gh + '&fx=' + this.data.options.fx + '&wxh=' + this.data.options.wxh
            })
          }
        } else if (type == '19xcx' && link != '') {
            if (appid == 'wxca86930ec3e80717') {
                let {
                    owner,
                    orgn,
                    erp
                } = this.data.getIdentity
                erp = erp ? erp : 12;
                orgn = orgn ? orgn : 13;
                owner = owner ? owner : 20588;
                if (link.indexOf('&') >= 0) {
                    link = link.substring(0, link.indexOf('&'))
                }
                link = `${link}&owner=${owner}&orgn=${orgn}&erp=${erp}`
            }
            xhs.navigateToMiniProgram({
                appId: appid,
                path: link,
                envVersion: 'release'
            })
        }
    },
    // 处理多公告情况
    processString(gglink) {
        // 通过逗号分隔输入字符串，以分离URL-名称对
        let pairs = gglink.split(',').map(pair => pair.trim());
        // 创建一个数组来保存最终的对象
        let result = [];
        // 遍历对，提取URL和名称
        for(let i = 0;i<pairs.length;i++) {
            let pair = pairs[i]
            const [ href, name ] = pair.split('-')
            result.push({
                href,
                name
            })
        }
        return result;
    },
    // 关闭查看页面
    closechakan() {
        this.setData({
            chaxunbox: 0
        })
    },
    // 关闭公告汇总选择窗口
    closeGG(){
        this.setData({
            choiceGG: false
        })
    },
    // 打开具体公告链接
    openGG(e){
        const { url } = e.currentTarget.dataset;
        if(/files\.offcn\.com/.test(url)) {
          xhs.navigateTo({
            url: `/packageA/download/download?name=附件&url=${url}&type=file`
          })
        } else if(/pages/.test(url) || /package/.test(url)) {
          xhs.navigateTo({
            url: url + '&gh=' + this.data.options.gh + '&fx=' + this.data.options.fx + '&wxh=' + this.data.options.wxh
          })
        } else {
          xhs.navigateTo({
            url: `/packageB/gg/gg?link=${url}`,
          })
        }
    },
    // 选择岗位
    async choiceGw(e) {
        // console.log(this.data)
        if (this.data.myPhone != '') {
            if (!this.data.user) {
                await app.methods.registerCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, e.currentTarget.dataset.gwlb, '', this.data.book, 'chaxun')
            } else {
                await app.methods.updateuserCX(this.data.myPhone, this.data.form, this.data.laiyuan, (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, e.currentTarget.dataset.gwlb, '', 'chaxun', this.data.book, this.data.user.user)
            }
        }
        this.setData({
            gwlb: e.currentTarget.dataset.gwlb,
            bookDetails_box: !1
        })
    },
    async user_login(e){
        if(typeof e.detail == 'boolean') {
            this.setData({
                isShowAuth: e.detail
            })
        } else {
            this.setData({
                myPhone: e.detail
            })
            this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.form, 'chaxun'));
            this.thisMethod();
        }
    },
    handleContact(e) {
        console.log(e)
    },


    onShareAppMessage(){
        let { fx = 'hbwtb', gh = 'zcj37368', id } = this.data.options;
        let { imgUrl, notice, ggname } = this.data.item;
        console.log(fx, gh, id)
        console.log(imgUrl, notice, ggname)
        console.log(`/packageB/chaxun/chaxun?id=${id}&fx=${fx}&gh=${gh}`)
        return {
          title: notice ? notice : ggname,
          path: `/packageB/chaxun/chaxun?id=${id}&fx=${fx}&gh=${gh}`,
          imageUrl: imgUrl ? imgUrl : ''
        }
    },
})